package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dsteiner.widgets.client.IJsUtil;
import com.dsteiner.widgets.client.Widgets.IFlowPanel;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.domain.Distinct;
import com.dsteiner.widgets.client.domain.Sort;
import com.dsteiner.widgets.client.event.EventListener;
import com.dsteiner.widgets.client.event.EventManager;
import com.dsteiner.xelavos.client.SearchLink;
import com.dsteiner.xelavos.client.DashletWidget.DashletModel;
import com.dsteiner.xelavos.client.event.DistinctEvent;
import com.dsteiner.xelavos.client.event.SearchEvent;
import com.dsteiner.xelavos.client.event.SendMessageEvent;
import com.dsteiner.xelavos.client.model.MainTabModel;
import com.dsteiner.xelavos.client.model.SearchModel;
import com.dsteiner.xelavos.client.model.MainTabModel.Tab;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.ui.HTML;

public class DashletController {

	private DashletModel dashletModel;
	private IWidget overlayPanel;
	private ILazyWidget dashletWidget;
	private IJsUtil jsUtil;
	private SearchModel searchModel;
	private MainTabModel mainTabModel;
	private EventManager eventManager;
	private Map<String, IFlowPanel> moreContentPanels = new HashMap<String, IFlowPanel>();

	public void registerDashletModel(DashletModel dashletModel) {
		this.dashletModel = dashletModel;
		dashletModel.addPropertyChangeListener("visible", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateVisibility();
			}
		});
	}

	public void registerDashletWidget(ILazyWidget dashletWidget) {
		this.dashletWidget = dashletWidget;
		dashletWidget.addWidgetListener(new WidgetListenerAdapter() {
			@Override
			public void onAttach(IWidget widget) {
				updateVisibility();
				updateMoreSearchLinks();
			}
		});
	}

	public void registerExitToSiteLink(IHTML exitToSiteLink) {
		exitToSiteLink.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				dashletModel.setVisible(false);
			}
		});
	}

	public void registerJsUtil(IJsUtil jsUtil) {
		this.jsUtil = jsUtil;
		jsUtil.addWindowResizeHandler(new ResizeHandler(){
			public void onResize(ResizeEvent event) {
//				updatePosition();
			}
		});
	}

	public void registerMainTabModel(MainTabModel mainTabModel) {
		this.mainTabModel = mainTabModel;
	}

	public void registerOverlayPanel(IWidget overlayPanel) {
		this.overlayPanel = overlayPanel;
	}

	public void registerSearchModel(SearchModel searchModel) {
		this.searchModel = searchModel;
	}

	private void updateVisibility() {
		boolean visible = dashletModel.isVisible();
		overlayPanel.setVisible(visible);
	}

	public void registerEventManager(EventManager eventManager) {
		this.eventManager = eventManager;
		eventManager.registerListener(SearchEvent.class, new EventListener<SearchEvent>() {
			public void onEvent(SearchEvent event) {
				if(!event.getSearchRequest().isAll())
					dashletModel.setVisible(false);
			}
		});
		eventManager.registerListener(SendMessageEvent.class, new EventListener<SendMessageEvent>() {
			public void onEvent(SendMessageEvent event) {
				dashletModel.setVisible(false);
			}
		});
		eventManager.registerListener(DistinctEvent.class, new EventListener<DistinctEvent>() {
			public void onEvent(DistinctEvent event) {
				updateMoreSearchLinks();
			}
		});
	}

	private void updateMoreSearchLinks() {
		DistinctEvent event = eventManager.getEvent(DistinctEvent.class);
		if(event == null)
			return;
		
		for (IFlowPanel flowPanel : moreContentPanels.values()) {
			flowPanel.clear();
		}
		
		List<Distinct> distincts = event.getDistincts();
		for (Distinct distinct : distincts) {
			IFlowPanel flowPanel = moreContentPanels.get(distinct.getClassName());
			Tab tab = MainTabModel.getTab(distinct.getClassName());
			flowPanel.add(new SearchLink(distinct.getValue() +" ("+distinct.getCount()+")", distinct.getValue(), 
					tab, new Sort("registeredDate", false)));
		}
	}

	public void registerMoreContentPanel(IFlowPanel moreContentPanel, String className) {
		moreContentPanels.put(className, moreContentPanel);
		moreContentPanel.add(new HTML("cargando..."));
	}
}